from sqlalchemy.orm import Session

from SQLiteBase import engine, AutoConfig

# 添加配置
def add_config(ckey, cvalue, cdesc=""):
    with Session(engine) as session:
        # 先查询ckey是否存在
        kv_info =  session.query(AutoConfig).filter(AutoConfig.ckey == ckey).first()
        if kv_info is None:
            # 不存在，添加
            kv_info = AutoConfig(ckey=ckey, cvalue=cvalue, cdesc=cdesc)
            session.add(kv_info)
        else:
            # 存在，更新
            kv_info.cvalue = cvalue
            kv_info.cdesc = cdesc
        session.commit()

# 获取配置
def get_config(ckey):
    with Session(engine) as session:
        kv_info =  session.query(AutoConfig).filter(AutoConfig.ckey == ckey).first()
        if kv_info is None:
            return None
        else:
            return kv_info.cvalue

